<?php

declare(strict_types=1);
/**
 * Author: Zane
 * Email: 873934580@qq.com
 * Date: 2023/5/24.
 */

namespace App\Utils;

class ResultsVo
{
    private static $code = 0;

    public static function success($data = null, $message = ''): \Illuminate\Http\JsonResponse
    {
        $message = '' === $message ? __('common.success') : $message;
        $result = [
            'code' => self::$code,
            'message' => $message,
        ];

        if (null !== $data) {
            if (isset($data['total'], $data['data'])) {
                $result['data']['total'] = $data['total'];
                $result['data']['results'] = $data['data'];
            } else {
                $result['data'] = $data;
            }
        }

        return response()->json($result);
    }

    public static function error($error = '', $code = 10000): \Illuminate\Http\JsonResponse
    {
        if ($code === self::$code) {
            $code = self::$code + 1;
        }

        $result = [
            'code' => $code,
            'message' => $error,
        ];

        return response()->json($result);
    }
}
